#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <thread>

#define Max 2.0f
#define Min 1.0f

// 生成随机数据
typedef struct {
    int start;
    int end;
    float *arr;
}Mes;

void rand_gen(Mes *mes) {
    
    srand((int)time(NULL) + mes->start);
    
    for (int i = mes->start; i < mes->end; i++) {
        int r = rand();
        float f = (float)r / RAND_MAX;
        float f2 = f * Max - Min;
        mes->arr[i] = f2;
    }
    
}

int rand_arr(float *arr, int size) {
    int thread_num = 12;
    std::vector<std::thread> threads(thread_num);
    std::vector<Mes> mes(thread_num);
    int blocksdim;
    if (size%thread_num != 0) {
        if (size < thread_num) {
            for (int i = 0; i < size; i++) {
                int end = i + 1;
                int start = i;
                mes[i] = Mes{start, end, arr};
            }
        }else{
            blocksdim = size/(thread_num -1);
            for (int i = 0; i < thread_num; i++) {
                int end;
                if (i == thread_num - 1) {
                    end = size;
                }
                else{
                    end = blocksdim * (i + 1);
                }
                int start = blocksdim * i;
                
                mes[i] = Mes{start, end, arr};
            }
        }
    }else{
        blocksdim = size/thread_num;

        for (int i = 0; i < thread_num; i++) {
                int end = blocksdim * (i + 1);
                int start = blocksdim * i;
                mes[i] = Mes{start, end, arr};
            }
    }
    if (size < thread_num) {
        thread_num = size;
    }
    for (int i = 0; i < thread_num; i++) {
        threads[i] = std::thread(rand_gen, &mes[i]);
    }
    for (int i = 0; i < thread_num; i++) {
        threads[i].join();
    }

    return 0;
}

int main() {
    int size = 1000000;
    float *arr = new float[size];
    rand_arr(arr, size);

    for (int i = 0; i < 10; i++) {
        std::cout << arr[i] << std::endl;
    }
    
    delete[] arr;
}